package controller

import (
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"mail/app/config"
	"mail/app/model"
	"net/http"
)

type Admin struct {
	warpHttp
}

func (s *Admin) ForbiddenHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)
	if cont == nil {
		config.GetLogger().Warnw("用户ID不能为空",
			"cont", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("用户ID不能为空"))
		return
	}

	user := model.NewAdmin()

	err := user.Forbid(string(cont))
	if err != nil {
		config.GetLogger().Warnw("禁用用户账户错误",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(nil))
}

func (s *Admin) ForbiddenIPHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)
	if cont == nil {
		config.GetLogger().Warnw("禁用IP不能为空",
			"cont", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("禁用IP不能为空"))
		return
	}

	user := model.NewAdmin()

	err := user.ForbidIP(string(cont))
	if err != nil {
		config.GetLogger().Warnw("禁用IP错误",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(nil))
}

func (s *Admin) ForbiddenServiceHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)
	if cont == nil {
		config.GetLogger().Warnw("禁用用户ID和禁用服务类型不能为空",
			"cont", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("禁用用户ID和禁用服务类型不能为空"))
		return
	}

	user := model.NewAdmin()

	err := user.ForbidService(string(cont))
	if err != nil {
		config.GetLogger().Warnw("禁用用户服务错误",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(nil))
}

func (s *Admin) ForbiddenServerHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)
	if cont == nil {
		config.GetLogger().Warnw("禁用服务类型不能为空",
			"cont", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("禁用服务类型不能为空"))
		return
	}

	user := model.NewAdmin()

	err := user.ServiceControl(string(cont))
	if err != nil {
		config.GetLogger().Warnw("服务启停错误",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(nil))
}

func (s *Admin) GetUserListHandler(c *gin.Context) {
	user := model.NewAdmin()

	data, err := user.GetUserList()
	if err != nil {
		config.GetLogger().Warnw("获取用户列表错误",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *Admin) GetServerStatusHandler(c *gin.Context) {
	user := model.NewAdmin()

	data, err := user.CheckServiceStatus()
	if err != nil {
		config.GetLogger().Warnw("获取服务器服务状态错误",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *Admin) ResetUserPwdHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)
	if cont == nil {
		config.GetLogger().Warnw("用户ID不能为空",
			"cont", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("用户ID不能为空"))
		return
	}

	user := model.NewAdmin()

	err := user.ResetUserPwd(string(cont))
	if err != nil {
		config.GetLogger().Warnw("重置用户密码错误",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(nil))
}
